package cs.rsa.ts14.lima;

import java.io.*;

import org.apache.commons.io.*;

import cs.rsa.ts14.doubles.*;
import cs.rsa.ts14.framework.*;
import cs.rsa.ts14.india.IndiaLineTypeClassifierStrategy;
import cs.rsa.ts14.standard.StandardTimesagLineProcessor;

/**
 * 
 * Our command line processor for that outputs the required report (for exercise
 * H1, only Transport Overview report is implemented and tested).
 * 
 * @author Group Lima - RSA 2014
 * 
 */
public class TimeSag {
	public static void main(String[] args) throws IOException {
		// Validate arguments
		if (args.length != 2) {
			System.err.println("Usage: Timesag [type] [timesag file]");
			System.err.println("  type must be one of (W, C, T).");
			System.err
					.println("  timesag file must obey the specified format.");
			System.exit(-1);
		}

		String reportTypeString = args[0];
		File file = new File(args[1]);

		// Configure the Timesag processor based upon report type string.
		ReportBuilder builder = null;
		switch (reportTypeString) {
		case "W":
			System.err.println("Week overview report not supported");
			System.exit(-1);
			break;
		case "C":
			System.err.println("Category overview report not supported");
			System.exit(-1);
			break;
		case "T":
			builder = new TransportOverviewReportBuilder();
			break;

		}

		LineSequenceState sequenceState = new LineSequenceStateStub();
		TimesagLineProcessor tlp = new StandardTimesagLineProcessor(
				new IndiaLineTypeClassifierStrategy(), builder,
				sequenceState);

		// Create an iterator for the lines in the file
		LineIterator it = FileUtils.lineIterator(file, "UTF-8");
		tlp.beginProcess();
		try {
			while (it.hasNext()) {
				// process each line
				String line = it.nextLine();
				LineType linetype = tlp.process(line);
				if (linetype == LineType.INVALID_LINE) {
					System.out.println("Error in file: " + tlp.lastError());
					System.exit(-1);
				}
			}
		} finally {
			LineIterator.closeQuietly(it);
		}
		tlp.endProcess();
		
		// Ask the builder for the final report and print it.
		System.out.println(builder.getResult());
	}
}
